package com.fy56.platform.goodsTracking.police.api.impl;

import com.alibaba.fastjson.JSON;
import com.fy56.platform.goodsTracking.commons.DateUtil;
import com.fy56.platform.goodsTracking.commons.constant.JrptConstants;
import com.fy56.platform.goodsTracking.commons.security.MD5;
import com.fy56.platform.goodsTracking.commons.security.StringUtil;
import com.fy56.platform.goodsTracking.commons.unit.WebUtils;
import com.fy56.platform.goodsTracking.model.GoodstrackedRecordPublic;
import com.fy56.platform.goodsTracking.model.GoodstrackedRecordTopPublic;
import com.fy56.platform.goodsTracking.model.PoliceSet;
import com.fy56.platform.goodsTracking.model.SysCompany;
import com.fy56.platform.goodsTracking.police.api.PoliceUploadApi;
import com.fy56.platform.goodsTracking.police.service.PoliceSetService;
import com.fy56.platform.goodsTracking.police.service.SysCompanyService;
import com.fy56.platform.goodsTracking.police.unit.BuildJson;
import com.fy56.platform.goodsTracking.service.GoodstrackedRecordPublicServie;
import com.fy56.platform.goodsTracking.service.GoodstrackedRecordTopPublicService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by XTK on 2017-02-28.
 */
@Repository
public class PoliceUploadApiImpl implements PoliceUploadApi {
    static Logger log = Logger.getLogger(PoliceUploadApiImpl.class);
    @Autowired
    private GoodstrackedRecordTopPublicService topService;
    @Autowired
    private GoodstrackedRecordPublicServie subService;
    @Autowired
    private BuildJson buildJson;
    @Autowired
    private PoliceSetService policeSetService;

    private void doUpload(PoliceSet policeSet, String dataType, String info) {
        String importStr = "{'userName':'" + policeSet.getUser() + "','userPass':'" + MD5.digest(policeSet.getPass()) + "','dataType':'" + dataType + "','serviceSystem':'F6','callBackUrl':'','content':{" + info + "}}";
        JSON jsonObject = JSON.parseObject(importStr);
        importStr = "param=" + jsonObject;
        String rsp = "";
        try {
            rsp = WebUtils.doPost(JrptConstants.Police_Url, importStr, JrptConstants.CHARSET_UTF8, 10000, 10000);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        log.info("上传dataType" + dataType + ":" + importStr + " " + rsp);
    }

    @Override
    public void doUpload_ConsignmentBillMaster(PoliceSet policeSet, List<SysCompany> sysCompany) {
        Map<String, String> map = new HashMap<String, String>();
        for (SysCompany company : sysCompany) {
            map.put(company.getCompanyname(), company.getFycloudcode());
        }
        String endTime = DateUtil.Now();
        String beginTime= DateUtil.Now();
        if (!StringUtils.isEmpty( policeSet.getLast())){
             beginTime=policeSet.getLast();
        }
        //查询未上传货物跟踪主
        List<GoodstrackedRecordTopPublic> tops = topService.selectByNotUpload_Police(policeSet.getOrganizationcode(), beginTime, endTime);
        log.info(policeSet.getOrganizationcode() + ":公安平台需要上传订单:" + tops.size());
        for (GoodstrackedRecordTopPublic top : tops) {
//生成上传格式
            String consignmentBillMaster = buildJson.buildConsignmentBillMaster(top, map);
            doUpload(policeSet, "1", consignmentBillMaster);
        }
        //上传路由
        List<GoodstrackedRecordPublic> subs = subService.selectByNotUpload_Police(policeSet.getOrganizationcode(),beginTime, endTime);
        log.info(policeSet.getOrganizationcode() + ":公安平台需要上传路由:" + subs.size());

        for (GoodstrackedRecordPublic sub : subs) {
            //生成上传格式
            String consignmentBillMaster = buildJson.buildGoodstrackedRecord(sub);
            doUpload(policeSet, "2", consignmentBillMaster);
        }
        //更新公司发送时间
        policeSetService.updateTime("last", policeSet.getOrganizationcode());
    }

    @Override
    public void doUpload_Company(PoliceSet policeSet, List<SysCompany> sysCompanys) {

        for (SysCompany company : sysCompanys) {
            long dt = 0;
            String companyJson = "";
            if (StringUtils.isEmpty(policeSet.getCompany())) {
                dt = 1;
            } else {
                try {
                    dt = DateUtil.compare_date(DateUtil.dateToString(company.getCreatetime()), policeSet.getCompany());
                } catch (Exception e) {
                    log.info("上传公司信息计算日期出错:" + company.getCreatetime() + "  " + policeSet.getCompany());
                }
            }
            if (dt > 0) {
                companyJson = buildJson.buildCompany(company, "02");
            } else {
                if (null!=company.getEdittime()) {
                    try {
                        dt = DateUtil.compare_date(DateUtil.dateToString(company.getEdittime()), policeSet.getCompany());
                    } catch (Exception e) {
                        log.info("上传公司信息计算日期出错:" + company.getEdittime() + "  " + policeSet.getCompany());
                    }
                    if (dt > 0) {
                        companyJson = buildJson.buildCompany(company, "05");
                    }
                }
            }
            if (!companyJson.equals("")) {
                doUpload(policeSet, "3", companyJson);
            }
        }
        //更新公司发送时间
        policeSetService.updateTime("company", policeSet.getOrganizationcode());

    }

    @Override
    public void doUpload_Goods(PoliceSet policeSet) {
        if (StringUtils.isEmpty(policeSet.getGoods())) {
            String json = buildJson.buildGoods();
            doUpload(policeSet, "5", json);
            //更新公司发送时间
            policeSetService.updateTime("goods", policeSet.getOrganizationcode());
        }
    }


    @Override
    public void doUpload_Product(PoliceSet policeSet) {
        if (StringUtils.isEmpty(policeSet.getProduct())) {
            String json = buildJson.buildProduct();
            doUpload(policeSet, "6", json);
            //更新公司发送时间
            policeSetService.updateTime("product", policeSet.getOrganizationcode());
        }
    }

    public static void main(String args[]) {
        String str = "{'userName':'FYWL1','userPass':'123456','dataType':'3','serviceSystem':'F6','callBackUrl':'','content':''}";
        JSON json = JSON.parseObject(str);
        System.out.println(MD5.digest("123456"));
    }


}



